package com.sgb.kjwl.view.ui.activity.transport;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.net.Uri;
import androidx.recyclerview.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCarsInfoDetail3Binding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.transport.CarsInfoDetailEntity3;
import com.sgb.kjwl.view.ui.adapter.transport.CarsInfoDetailCarLinesAdapter3;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.transport.CarsInfoDetailModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import javax.inject.Inject;

public class CarsInfoDetailActivity3 extends AppActivity {

    private ActivityCarsInfoDetail3Binding binding;
    private CarsInfoDetailCarLinesAdapter3 adapter;

    @Inject
    CarsInfoDetailModel3 detailModel;

    public static void start(Context context, String id){
        Intent intent = new Intent(context,CarsInfoDetailActivity3.class);
        intent.putExtra("carsId",id);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent
                .builder()
                .appComponent(appComponent)
                .transportModule(new TransportModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_cars_info_detail3);
        binding.include.ivBack.setOnClickListener(view -> finish());
        binding.include.titleView.setText("车主详情");
        binding.kefuNumber.setOnClickListener(this);
        binding.carInfoDetailTel.setOnClickListener(this);

        adapter = new CarsInfoDetailCarLinesAdapter3(this,null);
        binding.carDriveLines.setLayoutManager(new GridLayoutManager(this,1));
        binding.carDriveLines.setAdapter(adapter);

    }

    @Override
    protected void init() {
        String id = getIntent().getStringExtra("carsId");
        detailModel.getCarsInfoDetail(id);
    }

    public void getCarsInfoDetail(CarsInfoDetailEntity3 entity){

        binding.setViewModel(entity);

        String url = entity.getCarImage();
        RequestOptions options = new RequestOptions()
                .centerCrop();
        Glide.with(CarsInfoDetailActivity3.this).asBitmap().apply(options).load(url).into(binding.carImg);


        if(null!= entity.getWays() && entity.getWays().size() >0){
            adapter.setDatas(entity.getWays());
        }

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.car_info_detail_tel:
                if(!TextUtils.isEmpty(detailModel.detailEntity.get().getContrctsNumber())){
                    showDialog(detailModel.detailEntity.get().getContrctsNumber());
                }else{
                    MToast.showToast(CarsInfoDetailActivity3.this,"空号");
                }
                break;
            case R.id.kefu_number:
                showDialog("400-001-0051");
                break;
        }
    }

    public void showDialog(String phone) {
        View view = View.inflate(this, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        if(TextUtils.isEmpty(phone)){
            tv_title.setText("温馨提示");
            tv_message.setText("认证后可拨打电话，去认证");
        }else{
            tv_title.setText("确认呼叫");
            tv_message.setText(phone);
        }

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(phone)){
                    //去认证

                }else{
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:" + phone));
                    startActivity(intent);
                }

                roundCornerDialog.dismiss();
            }
        });

        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }
}
